<?php

require_once 'Operations/Operation.php';

/**
 * Builds a list with all logs from a given user.
 */
class LogList extends Operation {
	/**
	 * Builds a list with all logs from a given user.
	 */
	public function execute() {
		// Get user:
		$userFactory = new UserFactory();
		$user = $userFactory->getCurrent();
		
		// User must be authorized:
		if (!$user->isAuthorized()) {
			throw new PCException("Unauthorized user.", 403);
		}
		
		// Get parameters:
		$protocol = HTTPHandler::getGetParam("protocol");
		$account = HTTPHandler::getGetParam("account");
		
		// Check input:
		if (!$protocol or !$account) {
			throw new PCException("Missing protocol or account.", 400);
		}
		
		// Get a list of logs:
		$logList = ChatlogTable::getList($user->getID(), $protocol, $account);
		
		// Display XML:
		Logger::log("LogList", "Building logList.");
		HTTPHandler::add("<logs>");
		if (is_array($logList) && count($logList)) {
			foreach ($logList as $log) {
				HTTPHandler::add($log->toXML());
			}
		}
		HTTPHandler::add("</logs>");
		Logger::log("LogList", "LogList built.");
		
		// Log activity:
		$logData["protocol"] = $protocol;
		$logData["account"] = $account;
		$logData["count"] = count($logList);
		Database::logActivity("LogList", $user->getID(), $user->getToken($_SERVER["REMOTE_ADDR"])->getTokenString(), serialize($logData));
	}
	
}